package com.clps.learning;

/**
 * @Program: clps-learning
 * @Description: signup and save
 * @author: Cooper
 * @date: 2021.10.12
 * @Version: 1.0
 */
import com.clps.learning.pojo.Admin;
import com.clps.learning.pojo.BaseUser;
import com.clps.learning.pojo.Customer;
import com.clps.learning.service.LoginService;
import com.clps.learning.service.SignUpService;
import com.clps.learning.service.impl.LoginServiceImpl;
import com.clps.learning.service.impl.SignUpServiceImpl;
import com.clps.learning.utils.RoleUtil;

import java.io.*;
import java.util.Scanner;

public class Application {

    private SignUpService signUpService = new SignUpServiceImpl();
    private LoginService loginService = new LoginServiceImpl();

    public void signUp(){
        Scanner scanner = new Scanner(System.in);
        // 输入用户名、密码、邮箱
        System.out.println("---------SignUp---------");
        System.out.println("SignUp For? 1.Admin 0.Customer");
        Integer role = Integer.parseInt(scanner.next());
        System.out.println("please input your username:");
        String username = scanner.next();
        System.out.println("please input your password:");
        String password = scanner.next();
        // 调用signUpService注册
        if(signUpService.signUp(username, password, role)){
            System.out.println("Congratulation, SignUp Successfully!");
        }else {
            System.out.println("SignUp Failed");
        }
    }

    public void login(){
        Boolean loginSuccess = false;
        Scanner scanner = new Scanner(System.in);
        // 输入用户名、密码、邮箱
        System.out.println("---------Login---------");
        System.out.println("please input your username:");
        String username = scanner.next();
        System.out.println("please input your password:");
        String password = scanner.next();
        System.out.println("please input your role num:  0.Customer, 1.Admin");
        Integer role = Integer.parseInt(scanner.next());
        if(role.equals(RoleUtil.CUSTOMER)){
            loginSuccess = loginService.login(new Customer(username, password));
        }else if(role.equals(RoleUtil.ADMIN)){
            loginSuccess = loginService.login(new Admin(username, password));
        }
        if(loginSuccess){
            System.out.println("Login Successfully");
        }else {
            System.out.println("Login Failed");
        }
    }

    public static void main(String[] args) throws IOException {
        Application application = new Application();
        application.signUp();
        application.login();
    }
}
